<template>
  <div>
    <div class="tabTitle">
      <button :style="{color:tab=='project'?'red':'black'}" @click="tab='project'">构建设置</button>
      <button :style="{color:tab=='schedule'?'red':'black'}" @click="tab='schedule'">定时任务</button>
      <button :style="{color:tab=='warning'?'red':'black'}" @click="tab='warning'">告警设置</button>
      <button style="float: right;" @click="addInfo">新增</button>
    </div>
    <div class="tabContent">
      <div v-show="tab=='project'" class="files">
        <table>
          <tr>
            <th style="width: 80px;">id</th>
            <th style="width: 150px;">名称</th>
            <th style="width: 150px;">客户端</th>
            <th>命令</th>
            <th style="width: 150px;">操作</th>
          </tr>
          <tr v-for="item in project">
            <td v-text="item.id"></td>
            <td v-text="item.name"></td>
            <td v-text="item.client"></td>
            <td>
              <p v-html="item.cmd.replaceAll('\n','<br>')"></p>
            </td>
            <td>
              <button @click="build(item)">构建</button>
              <button @click="editInfo(item)">编辑</button>
              <button @click="removeProject(item.id)">删除</button>
            </td>
          </tr>
        </table>
      </div>
      <div v-show="tab=='schedule'" class="files">
        <table>
          <tr>
            <th style="width: 80px;">id</th>
            <th style="width: 150px;">名称</th>
            <th style="width: 150px;">客户端</th>
            <th style="width: 150px;">服务名称</th>
            <th>执行时间</th>
            <th style="width: 120px;">类型</th>
            <th style="width: 80px;">命令</th>
            <th style="width: 80px;">状态</th>
            <th style="width: 120px;">操作</th>
          </tr>
          <tr v-for="item in schedule">
            <td v-text="item.id"></td>
            <td v-text="item.name"></td>
            <td v-text="item.client"></td>
            <td v-text="item.server"></td>
            <td>
              <span v-show="item.datetime.length<5">每十分钟</span>
              <span v-show="item.datetime.length==5">每小时</span>
              <span v-show="item.datetime.length==8">每天</span>
              <span v-show="item.datetime.length==11">每月</span>
              <span v-show="item.datetime.length==14">每年</span>
              <span v-show="item.datetime.length>4" v-text="item.datetime"></span>
            </td>
            <td v-text="item.type"></td>
            <td v-text="item.cmd"></td>
            <td v-text="item.enable>0?'启动':'禁用'"></td>
            <td>
              <button @click="editInfo(item)">编辑</button>
              <button @click="removeSchedule(item.id)">删除</button>
            </td>
          </tr>
        </table>
      </div>
      <div v-show="tab=='warning'" class="files">
        <table>
          <tr>
            <th style="width: 80px;">id</th>
            <th style="width: 150px;">名称</th>
            <th style="width: 150px;">客户端</th>
            <th>路径</th>
            <th style="width: 80px;">数量</th>
            <th style="width: 120px;">操作</th>
          </tr>
          <tr v-for="item in warning">
            <td v-text="item.id"></td>
            <td v-text="item.name"></td>
            <td v-text="item.client"></td>
            <td v-text="item.target"></td>
            <td v-text="item.number"></td>
            <td>
              <button @click="editInfo(item)">编辑</button>
              <button @click="removeWarning(item.id)">删除</button>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <div>
      cpu占用：computer.cpu.usage；
      磁盘空间：sysFiles.N.usage；
      内存占用：computer.mem.usage；
      tomcat内存：tomcatVos.N.perMemory；
      jar内存：jarVos.N.perMemory
    </div>
    <div :style="{display:infoOpen?'block':'none'}" @click="infoOpen=false" class="dialog">
      <div class="info" @click.stop>
        <div class="head">
          <span>{{ tab == 'project' ? '构建设置' : tab == 'schedule' ? '定时任务' : '告警设置' }}</span>
          <button @click="infoOpen=false" style="float: right;cursor: pointer;">关闭</button>
        </div>
        <div class="content">
          <div>名称：<input v-model="info.name"/></div>
          <div>客户端：
            <select v-model="info.client">
              <option v-for="item in list">{{ item.name }}</option>
            </select>
          </div>
          <div v-show="tab=='project'">
            <div>命令： <textarea class="long" v-model="info.cmd" rows="15"></textarea></div>
          </div>
          <div v-show="tab=='schedule'">
            <div>服务名：<input v-model="info.server"/></div>
            <div>执行时间：<input v-model="info.datetime"/></div>
            <div>类型：
              <select v-model="info.type">
                <option>tomcat</option>
                <option>jar</option>
                <option>service</option>
                <option>bat</option>
              </select>
            </div>
            <div>命令：
              <select v-model="info.cmd">
                <option v-show="info.type=='tomcat' || info.type=='service'">start</option>
                <option v-show="info.type=='tomcat' || info.type=='service'">stop</option>
                <option>restart</option>
              </select>
            </div>
            <div>启动：<input type="number" v-model="info.enable"/></div>
          </div>
          <div v-show="tab=='warning'">
            <div>路径：<input v-model="info.target"/></div>
            <div>数量：<input type="number" v-model="info.number"/></div>
          </div>
        </div>
        <div class="foot">
          <button @click="submit">提交</button>
        </div>
      </div>
    </div>
    <div v-show="buildReturn.length>0" style="padding: 10px;">
      <p v-for="r in buildReturn" v-text="r"></p>
    </div>
  </div>
</template>

<script>
import {getInfo, goCmd} from "@/api/index"
import {projectAdd, projectUpdate, removeProjectById, projectList} from "@/api/project"
import {scheduleAdd, scheduleUpdate, removeScheduleById, scheduleList} from "@/api/schedule"
import {warningAdd, warningUpdate, removeWarningById, warningList} from "@/api/warning"

export default {
  name: "Set",
  components: {},
  data() {
    return {
      list: {},
      tab: "project",
      info: {},
      project: [],
      schedule: [],
      warning: [],
      infoOpen: false,
      buildReturn: [],
    };
  },
  created() {
    getInfo().then(res => {
      this.list = res.data.sort((a, b) => a.sort - b.sort);
    });
    projectList().then(res => {
      this.project = res.data;
    });
    scheduleList().then(res => {
      this.schedule = res.data;
    });
    warningList().then(res => {
      this.warning = res.data;
    });
  },
  mounted() {
  },
  methods: {
    editInfo(info) {
      this.info = info;
      this.infoOpen = true;
    },
    addInfo() {
      this.info = {};
      this.infoOpen = true;
    },
    submit() {
      if (this.tab == "project") {
        if (this.info.id) {
          projectUpdate(this.info).then(res => {
            this.infoOpen = false;
            projectList().then(res => {
              this.project = res.data;
            });
          });
        } else {
          projectAdd(this.info).then(res => {
            this.infoOpen = false;
            projectList().then(res => {
              this.project = res.data;
            });
          })
        }
      }
      if (this.tab == "schedule") {
        if (this.info.id) {
          scheduleUpdate(this.info).then(res => {
            this.infoOpen = false;
            scheduleList().then(res => {
              this.schedule = res.data;
            });
          });
        } else {
          scheduleAdd(this.info).then(res => {
            this.infoOpen = false;
            scheduleList().then(res => {
              this.schedule = res.data;
            })
          })
        }
      }
      if (this.tab == "warning") {
        if (this.info.id) {
          warningUpdate(this.info).then(res => {
            this.infoOpen = false;
            warningList().then(res => {
              this.warning = res.data;
            });
          });
        } else {
          warningAdd(this.info).then(res => {
            this.infoOpen = false;
            warningList().then(res => {
              this.warning = res.data;
            })
          })
        }
      }
    },
    removeProject(id) {
      if (confirm("确定要删除这条记录吗？")) {
        removeProjectById(id).then(res => {
          projectList().then(res => {
            this.project = res.data;
          });
        })
      }
    },
    removeSchedule(id) {
      if (confirm("确定要删除这条记录吗？")) {
        removeScheduleById(id).then(res => {
          scheduleList().then(res => {
            this.schedule = res.data;
          });
        })
      }
    },
    removeWarning(id) {
      if (confirm("确定要删除这条记录吗？")) {
        removeWarningById(id).then(res => {
          warningList().then(res => {
            this.warning = res.data;
          });
        })
      }
    },
    build(item) {
      this.buildReturn = ["执行中..."];
      goCmd({
        clientName: item.client,
        typeEnum: "build",
        nginxValue: item.cmd
      }).then(res => {
        this.buildReturn = JSON.parse(res.msg);
      });
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

.tabTitle {
  padding: 10px;

  button {
    margin-right: 10px;
  }
}

.tabContent {
  padding: 0 10px;

  .files {
    border: 1px solid #cccccc;
    height: 600px;
    overflow: auto;
    padding: 10px;
  }
}

.dialog {
  background-color: rgba(0, 0, 0, 0.2);
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9999;

  .info {
    width: 800px;
    height: 500px;
    background-color: #FFFFFF;
    margin: 100px auto 0;
    border-radius: 20px;
    padding: 20px;
    position: relative;

    .head {
      width: 100%;
      border-bottom: 1px solid #cccccc;
      height: 40px;
      line-height: 40px;
    }

    .content {
      height: 420px;
      overflow: auto;
      text-align: right;

      div {
        height: 36px;
      }

      input, select, textarea {
        float: right;
        margin-right: 300px;
        width: 170px;
      }

      .long {
        width: 700px;
        margin-right: 10px;
      }
    }

    .foot {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      border-top: 1px solid #cccccc;
      height: 60px;
      line-height: 40px;
      text-align: center;
    }
  }
}

table {
  width: 100%;
  text-align: center;

  td, th {
    border: 1px solid #cccccc;
    padding: 5px;
  }
}

p {
  margin: 0;
  text-align: left;
}
</style>
